
var JsonUtil = cc.Class({

    properties: () => ({
        default: null
    }),

statics:{

    //如果加载resources下文件时， 路径以resources开头相对路径
    //如果加载getWritablePath()下文件时，path只需文件对应的文件名
    loadFile: function(path, isRes, callBack, errBack){
        if(isRes){
            path = cc.url.raw(path);
        }
        else if(cc.sys.isNative){
            let writablePath = jsb.fileUtils.getWritablePath();
            if(path.indexOf(writablePath) < 0)
            {
                path = writablePath+path;
            }
        }
        cc.loader.load(path, JSON, function(err, res){
            if(err){
                Log.error("-----load err: " + err);
                if(errBack){
                    errBack(err);
                }
            }else{
                if(callBack){
                    callBack(res);
                }
            }
        });
    },

    //写入json文件放到getWritablePath()下 
    writeFile: function(root, file){
        if(cc.sys.isNative)
        {
            let writablePath = jsb.fileUtils.getWritablePath();
            if(file.indexOf(writablePath) < 0)
            {
                file = writablePath+file;
            }
            jsb.fileUtils.writeToFile(root, file);
            cc.loader.release(file);
        }
    },

    writeStringToFile: function(string, file){
        if(cc.sys.isNative)
        {
            if(typeof string != 'string')
            {
                string = JSON.stringify(string);
            }
            let writablePath = jsb.fileUtils.getWritablePath();
            if(file.indexOf(writablePath) < 0)
            {
                file = writablePath+file;
            }
            jsb.fileUtils.writeStringToFile(string, file);
            cc.loader.release(file);
        }
    },
},

});


module.exports = JsonUtil;
